<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Amitav Roy
 * Date: 2/2/13
 * Time: 1:55 PM
 * To change this template use File | Settings | File Templates.
 */
class Content extends CI_Controller{
  // string for the url
  protected $url = 'justread/content/';

  public function __construct() {
    parent::__construct();
    // $this->output->enable_profiler(TRUE);
    $this->cache = $this->boost->get_boost_cache();
  }

  // this is the function to display a single node
  public function articles() {
    if (!$this->cache) {
      // building the alias to query the system url
      $seg3 = $this->uri->segment(3);
      $seg4 = $this->uri->segment(4);
      $alias = $this->url . $seg3 . '/' . $seg4;

      $url = $this->alias_model->get_system_url($alias);

      if (!$url) {redirect('/', 'location', 301);}

      $nid = explode('/', $url); // exploding node/nid
      $nid = $nid[1]; // taking the second part of the array

      // loading node model
      $this->load->model('nodes/node_model', 'node');

      // adding the scripts
      $this->carabiner->js(array('nodes/shCore.js'));
      $this->carabiner->js(array('nodes/shBrushPhp.js'));
      $this->carabiner->js(array('subscription/sub_mod.js'));
      $this->carabiner->js(array('subscription/sub_app.js'));

      // data content
      $node = $this->node->node_load($nid);
      $node = $node[0];

      // header and meta data
      $data['header']['pagetitle'] = $node['title'];

      // og meta properties
      $data['header']['metaprop']['og:title'] = $node['title'];
      $data['header']['metaprop']['og:type'] = 'article';
      $data['header']['metaprop']['og:url'] = get_og_url($alias);
      $data['header']['metaprop']['og:image'] = get_og_image($nid);
      $data['header']['metaprop']['og:site_name'] = 'Amitav Roy';
      $data['header']['metaprop']['og:description'] = $node['teaser'];
      
      // normal meta properties
      $data['header']['metadata']['keywords'] = $node['keywords'];
      $data['header']['metadata']['description'] = $node['teaser'];

      // twitter card meta properties
      $data['header']['metadata']['twitter:card'] = 'summary';
      $data['header']['metadata']['twitter:site'] = '@amitavroy7';
      $data['header']['metadata']['twitter:title'] = trim_text_with_dots($node['title'], 70);
      $data['header']['metadata']['twitter:description'] = trim_text_with_dots($node['teaser'], 200);
      $data['header']['metadata']['twitter:creator'] = '@amitavroy7';
      $data['header']['metadata']['twitter:image:src'] = get_image_url('article_banner', $node['uri']);
      $data['header']['metadata']['twitter:domain'] = 'http://amitavroy.com';

      // node content
      $data['content']['view'] = 'nodes/nodefull';
      $data['content']['data'] = $node;

      // calling the page template
      $page_html = $this->load->view('pages/page', $data, true);
      $this->boost->set_boost_cache($page_html);
    }
  }

  // this is generic term landing page
  public function term() {
    // adding the scripts
    $this->carabiner->js(array('subscription/sub_mod.js'));
    $this->carabiner->js(array('subscription/sub_app.js'));

    // getting the pagination settings
    $this->load->library('pagination');

    // building the alias to query the system url
    $seg3 = $this->uri->segment(3);
    $seg4 = $this->uri->segment(4);
    $alias = $this->url . $seg3 . '/' . $seg4;

    $url = $this->alias_model->get_system_url($alias);
    $tid = explode('/',$url);
    $tid = $tid[count($tid) - 1];

    if (!$url) {
      redirect('pagenotfound/index/1');
    }

    $this->load->model('taxonomy/taxonomy_model', 'taxonomy');
    $this->load->model('nodes/node_model', 'node');
    $this->load->model('pages/page_model', 'page');
    $this->load->model('taxonomy_term_data_model', 'ttd');

    $nids = $this->taxonomy->term_article_list($tid);
    $term = $this->ttd->get($tid);

    foreach ($nids as $nid) {
      $nodes[] = $this->node->node_load($nid['nid']);
    }

    $node_count = $this->page->term_based_article_count($tid); //dsm($node_count);
    $data['page_h1'] = 'Posts Tagged "' . $term['name'] . '"';
    $data['content']['view'] = 'pages/landingpage';
    $data['content']['data']['nodes'] = $nodes;
    $data['content']['data']['nodes_count'] = $node_count;
    $data['content']['data']['termpage'] = 5; // setting so that pagination uri is 5 (otherwise wrong active link)
    $data['content']['data']['more_url'] = 'justread/content/termmore/'.$seg4; // for pagination

    // loading the page layout
    $this->load->view('pages/page',$data);
  }

  // this is the term more page
  public function termmore() {
    // adding the scripts
    $this->carabiner->js(array('subscription/sub_mod.js'));
    $this->carabiner->js(array('subscription/sub_app.js'));

    // getting the pagination settings
    $this->load->library('pagination');

    // building the alias to query the system url
    $seg3 = $this->uri->segment(3);
    $seg4 = $this->uri->segment(4);
    $alias = $this->url . $seg3 . '/' . $seg4;
    $alias = str_replace('termmore', 'term', $alias);

    $url = $this->alias_model->get_system_url($alias);
    $tid = explode('/',$url);
    $tid = $tid[count($tid) - 1];
    
    if (!$url) {show_404();}

    $this->load->model('pages/page_model', 'page');
    $this->load->model('taxonomy/taxonomy_model', 'taxonomy');
    $this->load->model('nodes/node_model', 'node');
    $this->load->model('taxonomy_term_data_model', 'ttd');
    
    $nids = $this->page->get_term_based_nodes($tid, $this->uri->segment(5));
    $term = $this->ttd->get($tid);

    foreach ($nids as $nid) {
      $nodes[] = $this->node->node_load($nid['nid']);
    }

    $node_count = $this->page->term_based_article_count($tid); //dsm($node_count);
    $data['page_h1'] = 'Posts Tagged "' . $term['name'] . '"';
    $data['content']['view'] = 'pages/landingpage';
    $data['content']['data']['nodes'] = $nodes;
    $data['content']['data']['nodes_count'] = $node_count;
    $data['content']['data']['termpage'] = 5; // setting so that pagination uri is 5 (otherwise wrong active link)
    $data['content']['data']['more_url'] = 'justread/content/termmore/'.$seg4; // for pagination

    // loading the page layout
    $this->load->view('pages/page',$data);
  }

  // this is the landing page for web articles
  public function webarticles() {
    // adding the scripts
    $this->carabiner->js(array('subscription/sub_mod.js'));
    $this->carabiner->js(array('subscription/sub_app.js'));

    $this->load->library('pagination');
    $this->load->model('pages/page_model', 'page');

    $nodes = $this->page->get_webarticles_nodes();
    $node_count = $this->page->get_sec_article_count(3); //dsm($node_count);

    // data content
    $data['page_h1'] = 'Posts Tagged "Web"';
    $data['header']['pagetitle'] = 'Web Articles tutorials, articles on jQuery, PHP, Javascript';
    $data['content']['view'] = 'pages/landingpage';
    $data['content']['data']['nodes'] = $nodes;
    $data['content']['data']['nodes_count'] = $node_count;
    $data['content']['data']['more_url'] = 'justread/content/webmore'; // for pagination

    // loading the page layout
    $this->load->view('pages/page',$data);
  }

  // this is for the links of web articles
  public function webmore() {
    // adding the scripts
    $this->carabiner->js(array('subscription/sub_mod.js'));
    $this->carabiner->js(array('subscription/sub_app.js'));

    $this->load->library('pagination');
    $this->load->model('pages/page_model', 'page');

    $pager = $this->uri->segment(4); // dsm($pager);

    $node_count = $this->page->get_sec_article_count(3); //dsm($node_count);

    // data content
    $data['page_h1'] = 'Posts Tagged "Web"';
    $data['header']['pagetitle'] = 'Web Articles tutorials, articles on jQuery, PHP, Javascript';
    $data['content']['view'] = 'pages/landingpage';
    $data['content']['data']['nodes'] = $this->page->get_webarticles_nodes($pager);
    $data['content']['data']['nodes_count'] = $node_count;
    $data['content']['data']['more_url'] = 'justread/content/webmore'; // for pagination

    // loading the page layout
    $this->load->view('pages/page',$data);
  }

  public function travelarticles() {
    // adding the scripts
    $this->carabiner->js(array('subscription/sub_mod.js'));
    $this->carabiner->js(array('subscription/sub_app.js'));

    $this->load->library('pagination');
    $this->load->model('pages/page_model', 'page');

    $nodes = $this->page->get_travel_articles();
    $node_count = $this->page->get_sec_article_count(2); 
    // dsm($node_count);

    // data content
    $data['page_h1'] = 'Posts Tagged "Travel"';
    $data['header']['pagetitle'] = 'Travel articles, photographs and trip logs';
    $data['content']['view'] = 'pages/landingpage';
    $data['content']['data']['nodes'] = $nodes;
    $data['content']['data']['nodes_count'] = $node_count;
    $data['content']['data']['more_url'] = 'justread/content/webmore'; // for pagination

    // loading the page layout
    $this->load->view('pages/page',$data);
  }

  public function travelmore() {
    // adding the scripts
    $this->carabiner->js(array('subscription/sub_mod.js'));
    $this->carabiner->js(array('subscription/sub_app.js'));

    $this->load->library('pagination');
    $this->load->model('pages/page_model', 'page');

    // data content
    $data['header']['pagetitle'] = 'Travel articles, photographs and trip logs';
    $data['content']['view'] = 'pages/landingpage';
    $data['content']['data']['nodes'] = $this->page->get_travel_articles();

    // loading the page layout
    $this->load->view('pages/page',$data);
  }

}
